/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: ticketLink.c
> Author: KAIJIE.JIANG
> Mail: 
> Created Time: 2024年07月17日 星期三 05时32分55秒
***************************************************/

#include "ticketLink.h"


//申请头节点
TIC *createTicketHead(void){
	TIC *head = NULL;
	head = (TIC*)malloc(sizeof(TIC));
	memset(head,0,sizeof(TIC));
	return head;
}

//创建链表
TIC *createTicketLink(TIC *head){
	assert(head);
	TIC *cur = NULL;
	cur = head;
	TIC *new = NULL;

	int n = 0;
	while(1){
		printf("input new ticket id:\n");
		printf("id = 0时，完成航班信息录入\n");
		printf("ticket id:");
		scanf("%d",&n);

		if(n == 0){
			break;
		}

		new = (TIC*)malloc(sizeof(TIC));
		memset(new,0,sizeof(TIC));

		//输入ticket信息
		if(n >= 99999 && n < 1000000){
			new->data.id = n;
		}else{
			printf("id input error!\n");
			continue;
		}
		printf("起点:");
		scanf("%s",new->data.origin);
		printf("终点:");
		scanf("%s",new->data.finish);
		printf("出发日期:");
		scanf("%s",new->data.date);
		printf("票价：");
		scanf("%f",&new->data.price);
		printf("余票:");
		scanf("%d",&new->data.left);
		printf("总票:");
		scanf("%d",&new->data.gross);

		cur->next = new;
		new->next = NULL;
		cur = cur->next;
	}

	return head;
}

//删除链表
TIC *delTicketLink(TIC *head,int n){
	assert(head);
	TIC * cur = head->next;
	TIC * bef = head;

	printf("n=%d\n",n);
	while(cur != NULL){
		if(cur->data.id == n){
			break;
		}
		bef = cur;
		cur = cur->next;
	}
	bef->next = cur->next;
	free(cur);
	cur = NULL;
	return head;	
}

//修改链表
TIC *amendTicketLink(TIC *head,int n){
	assert(head);
	TIC * cur = NULL;
	cur = head;

	int model = 0;
	int num = 0;
	float num1 = 0;
	char str[20] = {0};
	while(cur != NULL){
		if(cur->data.id == n){
			while(1){
				num = 0;
				bzero(str,20*sizeof(char));
				printf("1--修改起点\n");
				printf("2--修改终点\n");
				printf("3--修改出发日期\n");
				printf("4--修改票价\n");
				printf("5--修改余票\n");
				printf("6--修改总票\n");
				printf("0--退出\n");

				printf("请选择需要修改的项目：\n");
				printf("model=");
				scanf("%d",&model);

				if(model == 0){
					break;
				}
				switch(model){
					case 1:
						bzero(cur->data.origin,20*sizeof(char));
						printf("修改后的起点：");
						scanf("%s",str);
						strcpy(cur->data.origin,str);
						break;
					case 2:
						bzero(cur->data.finish,20*sizeof(char));
						printf("修改后的终点：");
						scanf("%s",str);
						strcpy(cur->data.finish,str);
						break;
					case 3:
						bzero(cur->data.date,20*sizeof(char));
						printf("修改后的出发日期：");
						scanf("%s",str);
						strcpy(cur->data.date,str);
						break;
					case 4:
						printf("修改后的票价：");
						scanf("%f",&num1);
						cur->data.price = num1;
						break;
					case 5:
						printf("修改后的余票：");
						scanf("%d",&num);
						if(num <= cur->data.gross){
							cur->data.left = num;
						}else{
							printf("请保持余票小于总票！\n");
						}
						break;
					case 6:
						printf("修改后的总票：");
						scanf("%d",&num);
						cur->data.gross = num;
						break;
					default:
						printf("输入错误！\n");
						break;
				}
			}

			break;
		}
		cur = cur->next;
	}
	return head;	
}
//链表输出函数
void outputTicketLink(const TIC *head){
	assert(head);
	TIC *cur = NULL;
	cur = head->next;

	int i = 0;
	int *point = NULL;
	while(cur != NULL){
		printf("id:%d\torigin:%s\tfinish:%s\tdate:%s\tprice:%.2f\tleft:%.d\tgross:%d\n",\
				cur->data.id,cur->data.origin,cur->data.finish,cur->data.date,\
				cur->data.price,cur->data.left,cur->data.gross);
		point = cur->data.user;
		printf("乘客id如下：");
		for(i = 0; i < cur->data.gross; i++){
			if(i % 5 == 0){
				printf("\n");
			}
			printf("%d：%d\t",i,*(point+i));
		}
		printf("\n");
		cur = cur->next;
	}
}

//链表释放函数
void freeTicketLink(TIC *head){
	assert(head);
	TIC *tmp = NULL;
	TIC *cur = NULL;
	cur = head;

	while(cur != NULL){
		tmp = cur->next;
		free(cur);
		cur = NULL;
		cur = tmp;
	}
}

//会员信息写入文件函数
FILE *ticketWriteToFile(FILE *fp,const TIC *head){
	assert(head);
	TIC *cur = NULL;
	cur = head->next;

	while(cur != NULL){
		fwrite(cur,sizeof(TIC),1,fp);
		cur = cur->next;
	}
	cur = NULL;
	return fp;
}

//会员信息读出文件函数
TIC *ticketReadFile(FILE *fp,TIC *head){
	assert(head);
	TIC *cur = NULL;
	cur = head;
	TIC *new = NULL;

	size_t ld = 1;
	while(ld != 0){
		new = (TIC*)malloc(sizeof(TIC));
		memset(new,0,sizeof(TIC));

		ld = fread(new,sizeof(TIC),1,fp);

		if(ld == 0){
			free(new);
			new = NULL;
			break;
		}
		cur->next = new;
		new->next = NULL;
		cur = cur->next;
	}

	return head;
}
